const util = require('../../utils/util');
const DictService = require('../../service/system/dict.service');

//****字典类型 */
// 创建字典类型
async function createType(ctx) {
	const { userId } = util.decodeToken(ctx);
	const { typeName, typeCode, remark, type } = ctx.request.body;

	// 校验必填字段
	if (!typeName || !typeCode || !type) {
		return util.fail(ctx, '类型名称、类型码和类型不能为空');
	}

	// 检查 type_code 是否已存在
	const exists = await DictService.checkTypeCodeExists(typeCode);
	if (exists) {
		return util.fail(ctx, '类型码已存在，不能重复创建');
	}

	const result = await DictService.createDictType({ typeName, typeCode, remark, create_id: userId, type });
	util.success(ctx);
}

// 获取字典类型列表
async function listTypes(ctx) {
	const { typeName = '', typeCode = '', page = 1, limit = 10 } = ctx.request.query;
	const result = await DictService.getDictTypeList({ typeName, typeCode, page: Number(page), limit: Number(limit) });
	util.success(ctx, {
		current: result.pagination.page,
		limit: result.pagination.limit,
		totalPage: Math.ceil(result.pagination.total / result.pagination.limit),
		total: result.pagination.total,
		rows: result.list,
	});
}

// 更新字典类型
async function updateType(ctx) {
	const { userId } = util.decodeToken(ctx);
	const { id, typeName, typeCode, remark } = ctx.request.body;
	if (!id) return util.fail(ctx, '字典类型 ID 不能为空');
	const result = await DictService.updateDictType(id, { typeName, typeCode, remark, update_id: userId });
	util.success(ctx);
}

// 删除字典类型
async function deleteType(ctx) {
	const { userId } = util.decodeToken(ctx);
	const { ids } = ctx.request.body;
	if (!ids) {
		return util.fail(ctx, '请提供有效的字典类型 ID 数组');
	}
	await DictService.deleteDictType(ids, userId);
	util.success(ctx);
}

//****字典值 */

// 创建字典值
async function createDict(ctx) {
	const { userId } = util.decodeToken(ctx);
	const { sysDictTypeId, codeName, alias, sort, remark, callbackShowStyle } = ctx.request.body;
	if (!sysDictTypeId || !codeName) {
		return util.fail(ctx, '字典类型 ID 和字典名称不能为空');
	}
	const result = await DictService.createDict({ sysDictTypeId, codeName, alias, sort, remark, create_id: userId, callbackShowStyle });
	util.success(ctx);
}

// 获取字典值列表
async function listDicts(ctx) {
	const { sysDictTypeId = '', codeName = '', page = 1, limit = 10 } = ctx.request.query;
	const result = await DictService.getDictList({ sysDictTypeId, codeName, page: Number(page), limit: Number(limit) });
	util.success(ctx, {
		current: result.pagination.page,
		limit: result.pagination.limit,
		totalPage: Math.ceil(result.pagination.total / result.pagination.limit),
		total: result.pagination.total,
		rows: result.list,
	});
}

// 更新字典值
async function updateDict(ctx) {
	const { userId } = util.decodeToken(ctx);
	const { id, codeName, alias, sort, remark, callbackShowStyle } = ctx.request.body;
	if (!id) return util.fail(ctx, '字典值 ID 不能为空');
	const result = await DictService.updateDict(id, { codeName, alias, sort, remark, update_id: userId, callbackShowStyle });
	util.success(ctx);
}

// 删除字典值
async function deleteDict(ctx) {
	const { userId } = util.decodeToken(ctx);
	const { ids } = ctx.request.body;
	if (!ids || !Array.isArray(ids) || ids.length === 0) {
		return util.fail(ctx, '请提供有效的字典值 ID 数组');
	}
	await DictService.deleteDict(ids, userId);
	util.success(ctx);
}

// 获取所有字典数据
async function getAllDicts(ctx) {
	try {
		const dicts = await DictService.getAllDicts();
		util.success(ctx, dicts);
	} catch (error) {
		util.fail(ctx, `获取字典数据失败: ${error.message}`);
	}
}

module.exports = { createType, listTypes, updateType, deleteType, createDict, listDicts, updateDict, deleteDict, getAllDicts };
